Skip to content

Refactor STDP and event ops for new brainevent binary backend#817

Merged
chaoming0625 merged 1 commit intomasterfrom
fix
Mar 11, 2026
Merged

Refactor STDP and event ops for new brainevent binary backend#817
chaoming0625 merged 1 commit intomasterfrom
fix

Conversation

@chaoming0625
Copy link
Member

@chaoming0625 chaoming0625 commented Mar 11, 2026

Summary by Sourcery

Update event-based computation and STDP weight update implementations to use new binary/event abstractions and aligned brainevent backends, while adjusting dependencies and minor docs.

Bug Fixes:

  • Align STDP weight update functions with binary pre/post update helpers for dense and CSR connectivity.
  • Update event-based matvec/matmat operations to use BinaryArray and JITCScalarR backends compatible with newer brainevent versions.

Enhancements:

  • Simplify numpy compatibility layer by dropping the unused fix wrapper.
  • Tidy initialization module header placement and add a small main check in brainpy.state for basic exports inspection.

Build:

  • Raise brainevent dependency to >=0.0.7 in pyproject.toml and requirements.txt and drop Python 3.10 classifier.

Documentation:

  • Clarify brainpy.state README title and correct installation command to use the brainpy.state package name.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 11, 2026

Reviewer's Guide

Updates STDP weight update functions to new binary-specific APIs, migrates brainevent event structures and JIT connectivity operators to their newer Binary/JITCScalar variants, adjusts numpy compatibility and packaging metadata, and refreshes state module docs and requirements to match the new brainevent release.

Sequence diagram for event_mv_prob_homo using BinaryArray and JITCScalarR

sequenceDiagram
    participant Caller
    participant BrainPyMath as brainpy_math_event_matvec
    participant BrainEvent as brainevent
    participant Backend as array_backend

    Caller->>BrainPyMath: event_mv_prob_homo(events, weight, conn_prob, seed, shape, outdim_parallel, transpose)
    BrainPyMath->>BrainPyMath: unwrap Array wrappers
    BrainPyMath->>BrainEvent: BinaryArray(events)
    BrainEvent-->>BrainPyMath: binary_events
    BrainPyMath->>BrainEvent: JITCScalarR((weight, conn_prob, seed), shape, corder)
    BrainEvent-->>BrainPyMath: csr_operator
    alt transpose_true
        BrainPyMath->>Backend: vector @ csr_operator.T
        Backend-->>Caller: result
    else transpose_false
        BrainPyMath->>Backend: vector @ csr_operator
        Backend-->>Caller: result
    end
Loading

Class diagram for updated STDP layers using binary weight update functions

classDiagram
    class Layer
    class SupportSTDP {
        +stdp_update(on_pre, on_post, w_min, w_max)
    }

    class Dense {
        +W
        +stdp_update(on_pre, on_post, w_min, w_max)
    }

    class Linear {
    }

    class DenseWeightLayer {
        +weight
        +stdp_update(on_pre, on_post, w_min, w_max)
    }

    class OneToOne {
        +weight
        +stdp_update(on_pre, on_post, w_min, w_max)
    }

    class CSRLayer {
        +weight
        +indices
        +indptr
        +_pre_ids
        +_post_indptr
        +w_indices
        +stdp_update(on_pre, on_post, w_min, w_max)
    }

    class STDPUpdateFunctions {
        +update_dense_on_binary_pre(weight, spike, trace, w_min, w_max)
        +update_dense_on_binary_post(weight, trace, spike, w_min, w_max)
        +update_csr_on_binary_pre(weight, indices, indptr, spike, trace, w_min, w_max, shape)
        +update_csr_on_binary_post(weight, pre_ids, post_indptr, w_indices, trace, spike, w_min, w_max, shape)
    }

    Layer <|-- Dense
    Layer <|-- DenseWeightLayer
    Layer <|-- OneToOne
    Layer <|-- CSRLayer

    SupportSTDP <|.. Dense
    SupportSTDP <|.. DenseWeightLayer
    SupportSTDP <|.. OneToOne
    SupportSTDP <|.. CSRLayer

    Dense <|-- Linear

    Dense ..> STDPUpdateFunctions : uses
    DenseWeightLayer ..> STDPUpdateFunctions : uses
    OneToOne ..> STDPUpdateFunctions : uses
    CSRLayer ..> STDPUpdateFunctions : uses
Loading

File-Level Changes

Change Details Files
Switch STDP weight updates to new binary-specific update helpers.
  • Replace dense_on_pre/dense_on_post with update_dense_on_binary_pre/update_dense_on_binary_post in Dense, Linear, OneToOne layers
  • Replace csr_on_pre/csr2csc_on_post with update_csr_on_binary_pre/update_csr_on_binary_post in CSR-based STDP layer
brainpy/dnn/linear.py
Migrate event/JIT connectivity matvec APIs to brainevent BinaryArray and JITCScalarR.
  • Wrap input events with BinaryArray instead of EventArray in event-based matvec and matrix ops
  • Use JITCScalarR instead of JITCHomoR for homogeneous probabilistic connectivity in matvec and weight matrix generation helpers
brainpy/math/jitconn/event_matvec.py
brainpy/math/jitconn/matvec.py
brainpy/math/event/csr_matmat.py
brainpy/math/event/csr_matvec.py
Update state package entry and README usage to align with brainpy.state packaging.
  • Add main guard printing LIF and all in state package init for local execution/debugging
  • Change install command in state README from brainpy_state to brainpy.state and simplify heading
brainpy/state/__init__.py
brainpy/state/README.md
Align initialization header, numpy compatibility exports, and packaging metadata with current support matrix.
  • Move coding header below license comment in initialize/base.py
  • Stop re-exporting numpy.fix from compat_numpy and remove it from all
  • Drop Python 3.10 classifier and bump brainevent dependency to >=0.0.7 in pyproject
  • Bump brainevent requirement to >=0.0.7 in requirements.txt
brainpy/initialize/base.py
brainpy/math/compat_numpy.py
pyproject.toml
requirements.txt

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chaoming0625
Copy link
Member Author

@sourcery-ai title

@github-actions github-actions bot added documentation Improvements or additions to documentation dependencies Pull requests that update a dependency file build labels Mar 11, 2026
@sourcery-ai sourcery-ai bot changed the title chore: update version to 2.7.7, enhance README and requirements, and … Update STDP and event-based ops for new brainevent backends Mar 11, 2026
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The if __name__ == '__main__': block in brainpy/state/__init__.py introduces side-effectful debug printing in a package __init__ module and should be removed to keep imports clean and predictable.
  • In brainpy/state/README.md the install command was changed to pip install brainpy.state -U, but PyPI packages cannot contain dots in their name, so this likely should remain brainpy_state (or whatever the actual distribution name is).
  • The encoding declaration in brainpy/initialize/base.py was moved below the license header, which makes it ineffective per PEP 263; either move # -*- coding: utf-8 -*- back to the first or second line or remove it entirely if using UTF-8 by default.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `if __name__ == '__main__':` block in `brainpy/state/__init__.py` introduces side-effectful debug printing in a package `__init__` module and should be removed to keep imports clean and predictable.
- In `brainpy/state/README.md` the install command was changed to `pip install brainpy.state -U`, but PyPI packages cannot contain dots in their name, so this likely should remain `brainpy_state` (or whatever the actual distribution name is).
- The encoding declaration in `brainpy/initialize/base.py` was moved below the license header, which makes it ineffective per PEP 263; either move `# -*- coding: utf-8 -*-` back to the first or second line or remove it entirely if using UTF-8 by default.

## Individual Comments

### Comment 1
<location path="brainpy/state/README.md" line_range="40" />
<code_context>

 ```bash
-pip install brainpy_state -U
+pip install brainpy.state -U
 ```

</code_context>
<issue_to_address>
**issue (bug_risk):** The `pip install brainpy.state` command may be incorrect; consider confirming the actual package name on PyPI.

Because this is user-facing install guidance, an incorrect distribution name will lead to failed installs. Please verify the actual PyPI package name (e.g., whether it should be `brainpy_state` or another valid name) and update the command accordingly.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


```bash
pip install brainpy_state -U
pip install brainpy.state -U
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The pip install brainpy.state command may be incorrect; consider confirming the actual package name on PyPI.

Because this is user-facing install guidance, an incorrect distribution name will lead to failed installs. Please verify the actual PyPI package name (e.g., whether it should be brainpy_state or another valid name) and update the command accordingly.

@chaoming0625
Copy link
Member Author

@sourcery-ai title

@sourcery-ai sourcery-ai bot changed the title Update STDP and event-based ops for new brainevent backends Refactor STDP and event ops for new brainevent binary backend Mar 11, 2026
@chaoming0625 chaoming0625 merged commit 64e31f2 into master Mar 11, 2026
19 checks passed
@chaoming0625 chaoming0625 deleted the fix branch March 11, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant